<?php

require_once('config/maptrick.conf.php');
require_once(SITE_ROOT . 'lib/SmartyMaptrick.class.php');
require_once(SITE_ROOT . 'class/logic/Registration.class.php');
require_once(SITE_ROOT . 'class/logic/LogicHoC.class.php');
require_once(SITE_ROOT . 'class/bean/Team.class.php');
require_once(SITE_ROOT . 'config/session.php');
require_once(SITE_ROOT . 'lib/SmartyPaginate.class.php');

$logic = LogicHoC::getInstance();
$smarty = new SmartyMaptrick();

if (count($_POST) > 0 && ($_POST["rnd"] != $_SESSION["rnd_maptrick"] || $_POST["rnd"] == "" || !isset($_POST["rnd"]))) {
	$_SESSION = array();
	$_POST = array();
}

if (preg_match("/^[A-Za-z_-]{1,32}$/", $_POST['hidden'])) $hidden = $_POST['hidden'];

if (isset($_GET['oauth_token']) && isset($_GET['oauth_verifier']) && count($_POST) == 0) {
	try {
		$register = new Registration();
		$register->connect($_GET['oauth_token'], $_GET['oauth_verifier']);
	}
	catch (Exception $e) {
		$smarty->assign('message', $e->getMessage());
	}
}

SmartyPaginate::connect();

$directlink = "http://" . $_SERVER['HTTP_HOST'] . "/hallofcool.php?";

if (((isset($_GET['index']) && count($_GET) == 1)) || ((isset($_GET['countrysort']) && count($_GET) == 1)) || count($_GET) == 0) {
	if (isset($_SESSION['country'])) $countryID = $_SESSION['country'];
	if (isset($_SESSION['division'])) $divisionID = $_SESSION['division'];
	if (isset($_SESSION['league'])) $leagueID = $_SESSION['league'];
	if (isset($_SESSION['alliance'])) $allianceID = $_SESSION['alliance'];
	if (isset($_SESSION['month'])) $month = $_SESSION['month'];
	if (isset($_SESSION['year'])) $year = $_SESSION['year'];
	if (isset($_SESSION['term'])) $term = $_SESSION['term'];
	if (isset($_SESSION['type'])) $type = $_SESSION['type'];
	if (isset($_GET['index'])) $index = 100*(floor($_GET['index']/100)) + 1;
	else if (isset($_SESSION['index'])) $index = $_SESSION['index'];
	if (isset($_GET['countrysort'])) $countrysort = $_GET['countrysort'];
	else if (isset($_SESSION['countrysort'])) $countrysort = $_SESSION['countrysort'];
}
else {
	if (isset($_GET['country'])) $countryID = $_GET['country'];
	if (isset($_GET['division'])) $divisionID = $_GET['division'];
	if (isset($_GET['league'])) $leagueID = $_GET['league'];
	if (isset($_GET['alliance'])) $allianceID = $_GET['alliance'];
	if (isset($_GET['month'])) $month = $_GET['month'];
	if (isset($_GET['year'])) $year = $_GET['year'];
	if (isset($_GET['term'])) $term = $_GET['term'];
	if (isset($_GET['type'])) $type = $_GET['type'];
	if (isset($_GET['countrysort'])) $countrysort = $_GET['countrysort'];
	if (isset($_GET['index'])) $index = 100*(floor($_GET['index']/100)) + 1;
	SmartyPaginate::reset();
}

if ($countryID == 0 || !preg_match("/^[0-9]{1,3}$/", $countryID)) {
	unset($_SESSION['country']);
	unset($countryID);
}
else {
	$_SESSION['country'] = $countryID;
	$directlink .= "country=" . $countryID . "&amp;";
}

if ($divisionID == 0 || !preg_match("/^[0-9]{1,2}$/", $divisionID)) {
	unset($_SESSION['division']);
	unset($divisionID);
}
else {
	$_SESSION['division'] = $divisionID;
	$directlink .= "division=" . $divisionID . "&amp;";
}

if ($leagueID == 0 || !preg_match("/^[0-9]{1,6}$/", $leagueID)) {
	unset($_SESSION['league']);
	unset($leagueID);
}
else {
	$_SESSION['league'] = $leagueID;
	$directlink .= "league=" . $leagueID . "&amp;";
}

if ($allianceID == 0 || !preg_match("/^[0-9]{1,7}$/", $allianceID)) {
	unset($_SESSION['alliance']);
	unset($allianceID);
}
else {
	$_SESSION['alliance'] = $allianceID;
	$directlink .= "alliance=" . $allianceID . "&amp;";
}

if ($month == 0 || !preg_match("/^[0-9]{1,2}$/", $month)) {
	unset($_SESSION['month']);
	unset($month);
}
else {
	$_SESSION['month'] = $month;
	$directlink .= "month=" . $month . "&amp;";
}

if ($year == 0 || !preg_match("/^[0-9]{1,4}$/", $year)) {
	unset($_SESSION['year']);
	unset($year);
}
else {
	$_SESSION['year'] = $year;
	$directlink .= "year=" . $year . "&amp;";
}

if (!isset($term) || !preg_match("/^[0-9]{1,1}$/", $term)) {
	$term = CONST_COOL;
}
$_SESSION['term'] = $term;
$directlink .= "term=" . $term . "&amp;";

if (!isset($type) || !preg_match("/^[0-9]{1,1}$/", $type)) {
	$type = CONST_AWAY;
}
$_SESSION['type'] = $type;
$directlink .= "type=" . $type . "&amp;";

if ($countrysort == "true") {
	$countrysort = true;
	$_SESSION['countrysort'] = true;
	$directlink .= "countrysort=true&amp;";
}
else {
	$countrysort = false;
	$_SESSION['countrysort'] = false;
}

if ($hidden == 'login') {
	try {
		$register = new Registration();
		$callback = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
		$url = $register->login($callback);
	}
	catch (Exception $e) {
		$smarty->assign('message', $e->getMessage());
	}
}
else if ($hidden == 'logout') {
	try {
		$register = new Registration();
		$register->logout();
	}
	catch (Exception $e) {
		$smarty->assign('message', $e->getMessage());
	}
}
else if ($hidden == 'update') {
	try {
		$logic->update($_POST['teamID']);
	}
	catch (IsBotException $ibe) {
		$smarty->assign('warning', "You don't really want to get the coolness ratings from a bot or an ownerless team, do you? If the former owner was in the Hall of Cool he isn't anymore now.");
	}
	catch (LeagueNotAvailableException $lnae) {
		$smarty->assign('warning', "The league the team is playing in couldn't be determinated. It wasn't saved/updated in the Hall of Cool. Please try again later!");
	}
	catch (Exception $e) {
		unset($_SESSION['login']);
		unset($_SESSION['login_team']);
		unset($_SESSION['HT']);
		$smarty->assign('message', 'Timeout!');
	}
}

if ($hidden == 'login') {
	header('Location: '. $url);
}
else {
	$smarty->assign('action', $_SERVER['PHP_SELF']);
	$smarty->assign('loggedIn', $_SESSION['login']);

	// assign the important attributes for the header
	$smarty->assign('title', 'Maptrick');
	$smarty->assign('hoc_active', 'active');

	$_SESSION["rnd_maptrick"] = hash_hmac("sha256", rand(1000000000, 9999999999), SCRIPT_PWD);
	$smarty->assign("rnd_maptrick", $_SESSION["rnd_maptrick"]);
	
	if ($_SESSION['login'] && isset($_SESSION['login_team'])) {
		$login_team = $_SESSION['login_team'];
		$smarty->assign('login_user', $login_team->getUserName());
		$smarty->assign('login_team', $login_team->getName());
		$smarty->assign('login_teamID', $login_team->getTeamID());
		if ($login_team->getLeague() != null) {
			$smarty->assign('login_leagueID', $login_team->getLeague()->getLeagueID());
			$smarty->assign('login_league', $login_team->getLeague()->getName());
		}
	}

	$smarty->assign('countries', $logic->getCountries());
	$smarty->assign('divisions', $logic->getDivisions());

	if (isset($leagueID)) {
		$league = $logic->getLeague($leagueID);
		if (isset($league)) {
			$smarty->assign('selected_country', $league->getDivision()->getCountry()->getCountryID());
			$smarty->assign('divisions', $logic->getDivisions($league->getDivision()->getCountry()->getCountryID()));
			$smarty->assign('selected_division', $league->getDivision()->getDivisionID());
			$smarty->assign('leagues', $logic->getLeagues($league->getDivision()->getCountry()->getCountryID(), $league->getDivision()->getDivisionID()));
			$smarty->assign('selected_league', $league->getLeagueID());
		}
	}
	else if (isset($countryID) && isset($divisionID)) {
		$smarty->assign('selected_country', $countryID);
		$smarty->assign('divisions', $logic->getDivisions($countryID));
		$smarty->assign('selected_division', $divisionID);
		$smarty->assign('leagues', $logic->getLeagues($countryID, $divisionID));
	}
	else if (isset($countryID)) {
		$smarty->assign('selected_country', $countryID);
		$smarty->assign('divisions', $logic->getDivisions($countryID));
	}
	else if (isset($divisionID)) {
		$smarty->assign('divisions', $logic->getDivisions());
		$smarty->assign('selected_division', $divisionID);
	}

	$smarty->assign('selected_alliance', $allianceID);
	$smarty->assign('alliances', $logic->getAlliances());

	$smarty->assign('selected_month', $month);
	$smarty->assign('months', $logic->getMonths());
	$smarty->assign('selected_year', $year);
	$smarty->assign('years', $logic->getYears($month));

	$smarty->assign('selected_term', $term);
	$smarty->assign('selected_type', $type);
	$smarty->assign('countrysort', $countrysort);

	$resultSize = $logic->getRankingCount($countryID, $divisionID, $leagueID, $allianceID, $month, $year);

	SmartyPaginate::setUrlVar("index");
	SmartyPaginate::setLimit(100);
	SmartyPaginate::setPageLimit(7);
	SmartyPaginate::setTotal($resultSize);
	SmartyPaginate::assign($smarty);

	if (isset($index)) SmartyPaginate::setCurrentItem($index);

	$smarty->assign('resultSize', $resultSize);
	$smarty->assign('hocTable', $logic->createHoCTable($term, $type, $countryID, $divisionID, $leagueID, $allianceID, $month, $year, SmartyPaginate::getCurrentIndex(), SmartyPaginate::getLimit(), $countrysort));

	if ($_SESSION['login'] && isset($_SESSION['login_team'])) {
		$login_team = $_SESSION['login_team'];
		$smarty->assign('login_teamID' , $login_team->getTeamID());
	}

	$smarty->assign('directlink', substr($directlink, 0, strlen($directlink) - 5));

	$smarty->display('header.tpl');
	$smarty->display('hallofcool.tpl');
	$smarty->display('footer.tpl');
}

?>